package co.edu.poli.ficb.agl.automatable.gui;

import java.awt.event.ItemEvent;

import org.apache.commons.collections15.Factory;

import co.edu.poli.ficb.agl.automatable.graph.Arista;
import co.edu.poli.ficb.agl.automatable.graph.GrafoAutomata2;
import co.edu.poli.ficb.agl.automatable.graph.Vertice;
import co.edu.poli.ficb.agl.automatable.modelo.automatas.Automata;
import edu.uci.ics.jung.visualization.RenderContext;
import edu.uci.ics.jung.visualization.control.EditingModalGraphMouse;

/**
 * 
 * @author Diego
 *
 */
public class PA2EditingModalGraphMouse extends EditingModalGraphMouse<Vertice, Arista> {

	private GrafoAutomata2 grafo;
	
	private Automata automata;
	
	private PA2LabelConfiguration labelConfiguration;

	public PA2EditingModalGraphMouse(Automata automata, RenderContext<Vertice, Arista> rc,
			Factory<Vertice> vertexFactory, Factory<Arista> edgeFactory, 
			PA2LabelConfiguration labelConfiguration, GrafoAutomata2 grafo) {
		super(rc, vertexFactory, edgeFactory);
		//System.out.println("automata:"+automata);
		this.automata = automata;
		this.labelConfiguration = labelConfiguration;
		this.grafo = grafo;
		//loadPlugins();
		labelEditingPlugin = new PA2LabelEditingGraphMousePlugin(grafo, automata, labelConfiguration);
		remove(popupEditingPlugin);
		popupEditingPlugin = new PA2PopupGraphMousePlugin(automata, vertexFactory, edgeFactory);
		add(popupEditingPlugin);
	}
	
	public void setViewingMode() {
		remove(pickingPlugin);
		remove(animatedPickingPlugin);
		remove(translatingPlugin);
		remove(scalingPlugin);
		remove(rotatingPlugin);
		remove(shearingPlugin);
		remove(editingPlugin);
		remove(labelEditingPlugin);
		remove(annotatingPlugin);
		remove(popupEditingPlugin);
	}
	
	private static boolean isMode(MyMode mode) {
		for (Mode m : Mode.values()) {
			if (m.name().equals(mode.name()))
				return true;
		}
		return false;
	}
	
    public void setMode(MyMode mode) {
		if (isMode(mode)) {
			super.setMode(Mode.valueOf(mode.name()));
		} else {
			if (mode == MyMode.VIEWING) {
				setViewingMode();
			}
			if(modeBox != null) {
				modeBox.setSelectedItem(mode);
			}
			fireItemStateChanged(new ItemEvent(this, ItemEvent.ITEM_STATE_CHANGED, mode, ItemEvent.SELECTED));
		}
	}

	public void setGrafo(GrafoAutomata2 grafo) {
		this.grafo = grafo;
	}

	public GrafoAutomata2 getGrafo() {
		return grafo;
	}

	public void setAutomata(Automata automata) {
		this.automata = automata;
	}

	public Automata getAutomata() {
		return automata;
	}

	public void setLabelConfiguration(PA2LabelConfiguration labelConfiguration) {
		this.labelConfiguration = labelConfiguration;
	}

	public PA2LabelConfiguration getLabelConfiguration() {
		return labelConfiguration;
	}    
}
